package com.zemb.flxwear.UI.Fragment;

import android.view.View;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.zemb.flxwear.Bean.ReportItem;
import com.zemb.flxwear.DB.DataBaseHelper;
import com.zemb.flxwear.R;
import com.zemb.flxwear.Utils.DeviceUtils;
import com.zemb.flxwear.Utils.LogUtils;
import com.zemb.flxwear.Utils.VoicePlayUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BloodOxygenFragment extends BaseFragment implements View.OnClickListener {

    TextView tv_pulse_rate_value, tv_blood_oxygen_value, tv_device_name;
    DataBaseHelper.BloodOxygen bloodOxygen;

     int measureTime =0;
    @Override
    int getLoadingPageId() {
        return R.id.loading_page;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_blood_oxygen;
    }

    @Override
    void onNotifyCallBack(byte[] data) {
        if (Byte.toUnsignedInt(data[10]) != 1) {
            LogUtils.PrintLog(isMeasure + " isMeasure");
            onMeasureStart();
            bloodOxygen.bloodOxygen = Byte.toUnsignedInt(data[8]);
            bloodOxygen.plus = Byte.toUnsignedInt(data[9]);
            //判断bloodOxygen和plus是否是数字
            tv_blood_oxygen_value .setText(bloodOxygen.bloodOxygen+"");
            tv_pulse_rate_value.setText(bloodOxygen.plus+"");
            measureTime++;
            if(measureTime>300){
                bloodOxygen.bloodOxygen = Byte.toUnsignedInt(data[8]);
                bloodOxygen.plus = Byte.toUnsignedInt(data[9]);
                tv_blood_oxygen_value .setText(bloodOxygen.bloodOxygen+"");
                tv_pulse_rate_value.setText(bloodOxygen.plus+"");
                onMeasureComplete();
                measureTime =0;
            }
        } else {
            LogUtils.PrintLog(isMeasure + " isMeasure");
            //手指脱离仪器 //防止调用两次
            isMeasure = false;
            measureTime =0;
        }
    }

    @Override
    protected void initData() {
        bloodOxygen = new DataBaseHelper.BloodOxygen();
    }



    @Override
    protected void initView(View root) {
        tv_pulse_rate_value = root.findViewById(R.id.tv_pulse_rate_value);
        tv_blood_oxygen_value = root.findViewById(R.id.tv_blood_oxygen_value);
        tv_device_name = root.findViewById(R.id.tv_device_name);
        if (activity.getCurrentDevice() != null) {
            tv_device_name.setText(activity.getCurrentDevice().getName());
        }
        root.findViewById(R.id.v_top_panel).setOnClickListener(this);
    }

    @Override
    public void onDeviceDisConnected(BleDevice bleDevice) {
        if(!isSuccessful&&isMeasure){
            onMeasureFailed();
        }
        VoicePlayUtils.getInstance().PlayDisconnect(activity.getMeasureTag());
        tv_device_name.setText(getResourceString(R.string.disconnect));
        isMeasure = false;
        hideLoading();
    }
    @Override
    public void onStartConnectDevice() {
        VoicePlayUtils.getInstance().PlayConnectStart(activity.getMeasureTag());
        showLoading("正在连接设备");
    }
    @Override
    public void onDeviceConnect(BleDevice bleDevice) {
        VoicePlayUtils.getInstance().PlayConnectSuccess(activity.getMeasureTag());
        hideLoading();
        openNotify();
        tv_device_name.setText(bleDevice.getName());

    }
    @Override
    public void onDeviceConnectFail(BleDevice bleDevice, BleException exception) {
        VoicePlayUtils.getInstance().PlayConnectFailed(activity.getMeasureTag());
        hideLoading();

    }
    @Override
    public void onMeasureComplete() {
        VoicePlayUtils.getInstance().PlayMeasureSuccess(activity.getMeasureTag());
        showLoading(getResourceString(R.string.measure_complete));
        closeNotify();
      //  ExoPlayerUtils.getInstance().playMeasureSuccess(activity.getMeasureTag());
        bloodOxygen.date = System.currentTimeMillis();
        DataBaseHelper.getInstance(activity).addBloodOxygen(bloodOxygen);
        if(isAuto){
            ReportItem item =new ReportItem();
            item.type = DeviceUtils.Category.BLOOD_OXYGEN;
            item.result = "H R "+bloodOxygen.plus+"\nB O "+bloodOxygen.bloodOxygen;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            item.time = sdf.format(new Date(bloodOxygen.date));
            activity.finishTask(item);
        }
        hideLoading();
        isMeasure = false;
        isSuccessful = true;
    }

    @Override
    public void onMeasureStart() {
        if (isMeasure||isSuccessful) {
            return;
        }
        VoicePlayUtils.getInstance().PlayMeasureStart(activity.getMeasureTag());
        showLoading(getResourceString(R.string.measuring));
        activity.wakeLock();
        isMeasure = true;

    }

    @Override
    public String getCharacteristicHead() {
        return "0000fff4";
    }

    @Override
    void closeNotify() {
        BleManager.getInstance().stopNotify(activity.getCurrentDevice(), service_uuid, characteristic_uuid);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_top_panel:
                activity.switchFragment(DeviceUtils.UIType.CONNECT);
                break;
        }
    }
}
